package main

import (
	"go-gtk3/utils"
	"log"
	"os"

	"github.com/gotk3/gotk3/glib"
	"github.com/gotk3/gotk3/gtk"
)

func main() {
	const appID = "com.github.gotk3.gotk3-examples"
	application, err := gtk.ApplicationNew(appID, glib.APPLICATION_FLAGS_NONE)
	if err != nil {
		log.Fatal("Could not create application:", err)
	}

	application.Connect("activate", func(f interface{}) {
		log.Printf("app.activate")
		window := newWindow(application)

		//窗口销毁事件
		window.Connect("destroy", func(f *gtk.ApplicationWindow) bool {
			log.Printf("window.destroy")
			return true
		})
		//窗口删除事件（点击右上角的关闭事件）
		window.Connect("delete_event", func(f *gtk.ApplicationWindow) bool {
			log.Printf("window.delete_event")
			alertBtn := utils.ShowCustomerAlert(window.ToWindow(), "警告", "确定退出？")
			log.Printf("alertBtn:%d", alertBtn)
			return alertBtn != 1
		})

		window.ShowAll()
	})

	os.Exit(application.Run(os.Args))
}

func newWindow(application *gtk.Application) *gtk.ApplicationWindow {
	win, _ := gtk.ApplicationWindowNew(application)
	win.SetTitle("GOTK3 Actions Example")

	header := createHeadBar()

	win.SetTitlebar(header)
	win.SetPosition(gtk.WIN_POS_MOUSE)
	win.SetDefaultSize(600, 300)
	return win
}

func createHeadBar() *gtk.HeaderBar {
	header, _ := gtk.HeaderBarNew()
	header.SetShowCloseButton(true)
	header.SetTitle("GOTK3")
	header.SetSubtitle("Actions Example")
	return header
}
